package driver.ui.driver;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import driver.ui.R;
import driver.ui.driver.Adapter.ActionlistAdapter;
import driver.ui.driver.Beans.action;
import android.content.DialogInterface;
import android.content.Intent;

public class Contactperson extends Activity{
    private PhoneStateListener mListener;
    private TelephonyManager mTelMgr;
	private ListView drivingactions;
	private ActionlistAdapter arrayAdapter;
	private TextView drivingname;
    
    private class CallEndedListener extends PhoneStateListener {
        boolean called = false;

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if (called && state == TelephonyManager.CALL_STATE_IDLE) {
                called = false;
                mTelMgr.listen(this, PhoneStateListener.LISTEN_NONE);
                try {
                    Contactperson.this.finish();
                    Intent t = new Intent(Contactperson.this, Contactperson.class);
                    t.setAction(Intent.ACTION_MAIN);
                    startActivity(t);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                if (state == TelephonyManager.CALL_STATE_OFFHOOK)
                    called = true;
            }
        }
    }
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.contact);
	        
	        Bundle extras = getIntent().getExtras();
			String name = extras.getString("name");			
	        drivingname=(TextView)findViewById(R.id.drivingname);
	        drivingname.setText(name);
	        
	        mListener = new CallEndedListener();
	        mTelMgr = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
	        
	        //Add actions
	        drivingactions = (ListView) findViewById(R.id.drivingactionList);
	        ArrayList<action>   actionlist= new ArrayList<action>();
	        //Initialize our array adapter notice how it references the contactlistitems.xml layout
	        arrayAdapter = new ActionlistAdapter(Contactperson.this, R.layout.actionitems,actionlist);
	        //Add actions
			action ac1=new action();
		    ac1.setImgsrc(R.drawable.call);
		    ac1.setName("Call(Emergency Only)");
		    actionlist.add(ac1);
		    action ac2=new action();
		    ac2.setImgsrc(R.drawable.sms);
		    ac2.setName("Send text message");
		    actionlist.add(ac2);
		    action ac3=new action();
		    ac3.setImgsrc(R.drawable.voicemail);
		    ac3.setName("Leave voicemail");
		    actionlist.add(ac3);
		    action ac4=new action();
		    ac4.setImgsrc(R.drawable.share);
		    ac4.setName("Share Status");
		    actionlist.add(ac4);
		    action ac5=new action();
		    ac5.setImgsrc(R.drawable.clock);
		    ac5.setName("Set up call reminder");
		    actionlist.add(ac5);

	        
	        //Set the above adapter as the adapter of choice for our list
	        drivingactions.setAdapter(arrayAdapter);
	        //Dialogs
	        final AlertDialog.Builder callblockBuilder=new AlertDialog.Builder(this);
	        final AlertDialog.Builder statusBuilder=new AlertDialog.Builder(this);
	        
	        drivingactions.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int position, long arg3) {
					// TODO Auto-generated method stub
					
						if(position==0)
						{
							callblockBuilder.setCancelable(true);
							callblockBuilder.setIcon(R.drawable.alert);
							callblockBuilder.setTitle("Emergency Only");
							callblockBuilder.setMessage("Alice is currently driving and will only take calls in an emergency situation. Is this an emergency call?");
						    callblockBuilder.setPositiveButton
						    ("Yes", new DialogInterface.OnClickListener() {
					               public void onClick(DialogInterface dialog, int id) {
					                   mTelMgr.listen(mListener, PhoneStateListener.LISTEN_CALL_STATE);
					            	   Intent callIntent = new Intent(Intent.ACTION_CALL, Uri
					                           .parse("tel:99999999"));
					                   startActivity(callIntent);
			                  
			            	
			               }
			           })
			           .setNegativeButton("No Thanks", new DialogInterface.OnClickListener() {
			               public void onClick(DialogInterface dialog, int id) {
			                    dialog.cancel();
			               }
			           });
				
					callblockBuilder.show();
	
					}
						else if(position==1)
						{
							Intent addIntent=new Intent();
							addIntent.setAction("Composemessage");
							startActivity(addIntent);
							
						}
						else if(position==3)
						{
							final CharSequence[] StatusTime = {"15 minutes", "30 minutes", "1 hour", "2 hour"};
							
							statusBuilder.setCancelable(true);
							statusBuilder.setIcon(R.drawable.timer);
							statusBuilder.setTitle("Share Status For");
							statusBuilder.setSingleChoiceItems(StatusTime, 0, new DialogInterface.OnClickListener ()
							{
								@Override

							    public void onClick(DialogInterface dialog, int id) {
								}

							});
						    statusBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {
					               public void onClick(DialogInterface dialog, int id) {		                    
					               }
					           })
					           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					               public void onClick(DialogInterface dialog, int id) {
					                    dialog.cancel();
					               }
					           });
						
							statusBuilder.show();
						
						}
				}});

	 }
	 

	        
	 }

		



